﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using System.IO;
using System.Windows.Forms;
namespace CommonLib
{
    public class AETools
    {
        #region 样点图层及其字段信息
        public static string YDLayerName = "";//样点图层名
        public static string YDLayerField = "";//样点图层标定地价字段名称
        //修正系数
        public static string RJLXZXS = "";
        public static string SYNQXZXS = "";
        public static string LJSDXZXS = "";
        public static string KSBXZXS = "";
        public static string ZDXZXZXS = "";
        public static string JJDXZXS = "";
        public static string KFCDXZXS = "";
        public static string EJFLXZXS = "";
       // public static string  
        #endregion
        

        public static string tagetLayerField = "";//宗地图层标定地价字段名
        public static string tagetLayerName = "";//宗地图层名
        public static string tagetCRJGField = "";//出让价格字段
        /// <summary>
        /// 变量:主地图控件
        /// </summary> 
        public static AxMapControl MapMainControl;
        /// <summary>
        /// 主地图上鼠标操作产生的Geometry
        /// </summary>
        public static IGeometry MapGeometry = null;
        /// <summary>
        /// 获得颜色
        /// </summary>
        /// <param name="RRGGBB">颜色值RRGGBB，如"ff0000"为红色</param>
        /// <returns></returns>
        public static IColor getIColor(string RRGGBB)
        {
            IRgbColor pRgbColor = new RgbColor();
            pRgbColor.Red = int.Parse(RRGGBB.Substring(0, 2), System.Globalization.NumberStyles.HexNumber);
            pRgbColor.Green = int.Parse(RRGGBB.Substring(2, 2), System.Globalization.NumberStyles.HexNumber);
            pRgbColor.Blue = int.Parse(RRGGBB.Substring(4, 2), System.Globalization.NumberStyles.HexNumber);
            return (IColor)pRgbColor;
        }

        /// <summary>
        /// 获取ISimpleMarkerSymbol
        /// </summary>
        /// <returns></returns>
        public static ISimpleMarkerSymbol getSimpleMarkerSymbol()
        {
            ISimpleMarkerSymbol pSimpleMarkerSymbol = new SimpleMarkerSymbol();
            pSimpleMarkerSymbol.Style = esriSimpleMarkerStyle.esriSMSDiamond;   //设置符号类型：钻石
            pSimpleMarkerSymbol.Color = CommonLib.AETools.getIColor("ff0000"); //设置颜色
            pSimpleMarkerSymbol.Size = 10;       //设置大小
            return pSimpleMarkerSymbol;
        }

        /// <summary>
        /// 获取ISimpleLineSymbol
        /// </summary>
        /// <returns></returns>
        public static ISimpleLineSymbol getSimpleLineSymbol()
        {
            ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbol();
            pSimpleLineSymbol.Color = CommonLib.AETools.getIColor("ff0000"); //设置颜色
            pSimpleLineSymbol.Width = 2;       //设置线宽
            pSimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSDashDot;//设置线型
            return pSimpleLineSymbol;
        }

        /// <summary>
        /// 获取ISimpleFillSymbol
        /// </summary>
        /// <returns></returns>
        public static ISimpleFillSymbol getSimpleFillSymbol()
        {
            ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
            pSimpleFillSymbol.Outline = CommonLib.AETools.getSimpleLineSymbol(); //外边线
            pSimpleFillSymbol.Color = CommonLib.AETools.getIColor("99ccff");     //设置填充色
            pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;  //设置填充方式
            return pSimpleFillSymbol;
        }

        /// <summary>
        /// 坐标转换：WGS84转Xian80
        /// </summary>
        /// <param name="longitude">经度</param>
        /// <param name="latitude">纬度</param>
        /// <returns></returns>
        public static IPoint WGS84ToXian80(double longitude, double latitude)
        {
            try
            {
                IPoint pt = new PointClass();
                pt.PutCoords(longitude, latitude);

                ISpatialReferenceFactory2 spatialReferenceFact = new SpatialReferenceEnvironmentClass();
                IGeographicCoordinateSystem pGeoWGS84 = spatialReferenceFact.CreateGeographicCoordinateSystem((int)esriSRGeoCSType.esriSRGeoCS_WGS1984);
                IProjectedCoordinateSystem pProjectXian80 = spatialReferenceFact.CreateProjectedCoordinateSystem((int)esriSRProjCS4Type.esriSRProjCS_Xian1980_3_Degree_GK_CM_114E);

                IGeometry geometry = (IGeometry)pt;
                geometry.SpatialReference = pGeoWGS84;
                geometry.Project(pProjectXian80);
                return pt;
            }
            catch
            {
                return null;
            }
        }

        public static void Map_ZoomTo(AxMapControl axMapControl, IGeometry myGeo)
        {
            if (axMapControl.Map != null && axMapControl.ActiveView != null && myGeo != null && !myGeo.IsEmpty)
            {
                if (myGeo.GeometryType == esriGeometryType.esriGeometryPoint)
                {
                    IPoint point = new PointClass();
                    IEnvelope envelope = myGeo.Envelope;
                    point.X = (envelope.XMin + envelope.XMax) / 2.0;
                    point.Y = (envelope.YMin + envelope.YMax) / 2.0;
                    if (envelope.XMax > 360 && envelope.YMax > 360)
                    {
                        envelope.Width = 1000;
                        envelope.Height = 1000;
                    }
                    else
                    {
                        envelope.Width = 0.01;
                        envelope.Height = 0.01;
                    }
                    envelope.CenterAt(point);
                    axMapControl.ActiveView.Extent = envelope;
                    axMapControl.ActiveView.Refresh();
                    Application.DoEvents();
                }
                else
                {
                    IPoint point = new PointClass();
                    IEnvelope envelope = myGeo.Envelope;
                    point.X = (envelope.XMin + envelope.XMax) / 2.0;
                    point.Y = (envelope.YMin + envelope.YMax) / 2.0;
                    if (envelope.XMax == 0.0 && envelope.YMax == 0.0)
                    {
                        envelope.Width = 10;
                        envelope.Height = 10;
                    }
                    else
                    {
                        envelope.Expand(1.25, 1.25, true);
                    }
                    envelope.CenterAt(point);
                    axMapControl.ActiveView.Extent = envelope;
                    axMapControl.ActiveView.Refresh();
                    Application.DoEvents();
                }
            }
        }

        /// <summary>
        /// 地图跳转到指定geometry并闪烁
        /// </summary>
        /// <param name="axMapControl"></param>
        /// <param name="geometry"></param>
        public static void mapJumpAndFlash(AxMapControl axMapControl, IGeometry geometry)
        {
            #region 地图跳转
            IEnvelope envelope = geometry.Envelope;
            //envelope.Expand(500, 500, true);
            double width = 0;
            double height = 0;
            if (geometry.GeometryType == esriGeometryType.esriGeometryPoint)
            {
                width = height = 800;
            }
            else
            {
                width = envelope.Width * 0.2;
                height = envelope.Height * 0.2;
            }
            envelope.XMin -= width;
            envelope.YMin -= height;
            envelope.XMax += width;
            envelope.YMax += height;
            axMapControl.ActiveView.Extent = envelope;
            //axMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, Type.Missing, axMapControl.ActiveView.Extent);
            //axMapControl.ActiveView.ScreenDisplay.UpdateWindow();
            #endregion
            #region 地图闪烁
            object symbol = null;
            switch (geometry.GeometryType)
            {
                case esriGeometryType.esriGeometryPoint:
                    symbol = CommonLib.AETools.getSimpleMarkerSymbol();
                    break;
                case esriGeometryType.esriGeometryLine:
                case esriGeometryType.esriGeometryPolyline:
                    symbol = CommonLib.AETools.getSimpleLineSymbol();
                    break;
                case esriGeometryType.esriGeometryPolygon:
                    symbol = CommonLib.AETools.getSimpleFillSymbol();
                    break;
            }
            if (symbol == null) return;
            axMapControl.FlashShape(geometry, 3, 300, symbol);
            #endregion
        }

        private static ILayer getLayerByName(IMap pMap, string LyrName)
        {
            ILayer pLayer = null;
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                ILayer pLayerT = pMap.get_Layer(i);
                if (pLayerT is IGroupLayer)
                    pLayer = getLayerByNameFromGroupLayer(pLayerT, LyrName);
                else if (pLayerT is IFeatureLayer && pMap.get_Layer(i).Name == LyrName)
                {
                    pLayer = pMap.get_Layer(i);
                    break;
                }
                else
                    continue;
            }
            return pLayer;
        }
        private static ILayer getLayerByNameFromGroupLayer(ILayer pLayer, string LayerName)
        {
            ILayer pLayerR = null;
            ICompositeLayer pGroupLayer = pLayer as ICompositeLayer;
            for (int i = 0; i < pGroupLayer.Count; i++)
            {
                ILayer pLayerT = pGroupLayer.get_Layer(i);
                if (pLayerT is IGroupLayer)
                    pLayerR = getLayerByNameFromGroupLayer(pLayerT, LayerName);
                else if (pLayerT is IFeatureLayer && pLayerT.Name == LayerName)
                    pLayerR = pLayerT;
                else
                    continue;
            }
            return pLayerR;
        }
        /// <summary>
        /// 通过图层名，获取栅格图层
        /// </summary>
        /// <param name="axMapControl">地图控件</param>
        /// <param name="layerName">图层名</param>
        /// <returns></returns>
        public static IRasterLayer GetRasterLayers(AxMapControl axMapControl, string layerName)
        {
            UID uid = new UIDClass();
            uid.Value = "{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF}";//RasterLayer
            IEnumLayer layers = axMapControl.Map.get_Layers(uid, true);
            layers.Reset();
            ILayer layer = null;
            while ((layer = layers.Next()) != null)
            {
                if (layer.Name == layerName)
                    return layer as IRasterLayer;
            }
            return null;
        }

        /// <summary>
        /// 通过图层名，获取矢量图层
        /// </summary>
        /// <param name="axMapControl">地图控件</param>
        /// <param name="layerName">图层名</param>
        /// <returns></returns>
        public static IFeatureLayer GetFeatureLayer(IMap pMap, string layerName)
        {
            UID uid = new UIDClass();
            uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";//FeatureLayer
            //{6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer （包括所有类型的图层）
            //{40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer
            //{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF} IRasterLayer
            //{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer
            //{34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer
            //{5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer
            //{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer
            //{EDAD6644-1810-11D1-86AE-0000F8751720} IGroupLayer 
            IEnumLayer layers = pMap.get_Layers(uid, true);
            layers.Reset();
            ILayer layer = null;
            while ((layer = layers.Next()) != null)
            {
                if (layer.Name == layerName)
                    return layer as IFeatureLayer;
            }
            return null;
        }

        /// <summary>
        /// 通过图层名，获取矢量图层
        /// </summary>
        /// <param name="axMapControl">地图控件</param>
        /// <param name="layerName">图层名</param>
        /// <returns></returns>
        public static IFeatureLayer GetFeatureLayer(AxMapControl axMapControl, string layerName)
        {
            UID uid = new UIDClass();
            uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";//FeatureLayer
            //{6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer （包括所有类型的图层）
            //{40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer
            //{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF} IRasterLayer
            //{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer
            //{34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer
            //{5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer
            //{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer
            //{EDAD6644-1810-11D1-86AE-0000F8751720} IGroupLayer 
            IEnumLayer layers = axMapControl.Map.get_Layers(uid, true);
            layers.Reset();
            ILayer layer = null;
            while ((layer = layers.Next()) != null)
            {
                if (layer.Name == layerName)
                    //layer.s
                    return layer as IFeatureLayer;
            }
            return null;
        }
        /// <summary>
        /// 根据图层的字段判断
        /// </summary>
        /// <param name="axMapControl"></param>
        /// <param name="layerName"></param>
        /// <returns></returns>
        public static IFeatureLayer GetFeatureLayer(AxMapControl axMapControl, string fieldName,int funcflag)
        {
            UID uid = new UIDClass();
            uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";//FeatureLayer
            IEnumLayer layers = axMapControl.Map.get_Layers(uid, true);
            layers.Reset();
            ILayer layer = null;
            while ((layer = layers.Next()) != null)
            {
                if((layer as IFeatureLayer).FeatureClass!=null&&(layer as IFeatureLayer).FeatureClass.Fields.FindField(fieldName)!=-1)
                    return layer as IFeatureLayer;
            }
            return null;
        }

        public static IFeatureLayer GetFeatureLayer(string shapefilePath, string shapefileName)
        {
            IPropertySet properties = new PropertySetClass();
            properties.SetProperty("DATABASE", shapefilePath);
            IWorkspaceFactory2 workspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspace = workspaceFactory.Open(properties, 0) as IFeatureWorkspace;
            if (featureWorkspace == null) return null;
            IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(shapefileName);
            if (featureClass == null) return null;
            IFeatureLayer layer = new FeatureLayerClass();
            layer.Name = shapefileName;
            layer.FeatureClass = featureClass;
            return layer;
        }

        public static IGeometry getCityGeometry(IFeatureLayer cityFeatureLayer, string cityName)
        {
            IQueryFilter queryFilter = new QueryFilter();
            queryFilter.WhereClause = "CityName ='" + cityName + "'";
            IFeatureCursor featureCursor = cityFeatureLayer.FeatureClass.Search(queryFilter, true);
            IFeature feature = featureCursor.NextFeature();
            return feature.Shape;
        }

        public static IGeometry getRegionGeometry(IFeatureLayer regionFeatureLayer, string regionName)
        {
            IQueryFilter queryFilter = new QueryFilter();
            queryFilter.WhereClause = "QXName ='" + regionName + "'";
            IFeatureCursor featureCursor = regionFeatureLayer.FeatureClass.Search(queryFilter, true);
            IFeature feature = featureCursor.NextFeature();
            return feature.Shape;
        }

        /// <summary> Shapefile文件-》FeatureLayer图层
        /// </summary>
        /// <param name="shapefileFullName"></param>
        /// <returns></returns>
        /// 
        public static IFeatureLayer GetFeatureLayerFromShapefile(string shapefileFullName)//获取shpfile的FeatureLayer
        {
            if (string.IsNullOrEmpty(shapefileFullName))
            {
                return null;
            }
            string pathName = shapefileFullName.Substring(0, shapefileFullName.LastIndexOf("\\"));
            string shapefileName = System.IO.Path.GetFileNameWithoutExtension(shapefileFullName);
            IPropertySet properties = new PropertySetClass();
            properties.SetProperty("DATABASE", pathName);
            IWorkspaceFactory2 workspaceFactory = new ShapefileWorkspaceFactoryClass();
            IFeatureWorkspace featureWorkspace = workspaceFactory.Open(properties, 0) as IFeatureWorkspace;
            if (featureWorkspace == null) return null;
            IFeatureClass featureClass = featureWorkspace.OpenFeatureClass(shapefileName);
            if (featureClass == null) return null;
            IFeatureLayer layer = new FeatureLayerClass();
            layer.Name = shapefileName;
            layer.FeatureClass = featureClass;
            return layer;
        }

        /// <summary>通过mdb返回FeatureClass
        /// </summary>
        /// <param name="workspacePath"></param>
        /// <returns></returns>
        public static IFeatureLayer GetmdbFeatureLayer(string workspacePath)
        {
            #region 一般情况读取mdb数据的空间信息
            //IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
            //IWorkspace pWorkSpace = workspaceFactory.OpenFromFile(workspacePath, 0);
            ////IWorkspace pWorkSpace = workspaceFactory.OpenFromFile(@"C:\Users\hn\Desktop\44广东省省农用地分等完善成果（2010年）\44-1县级成果\4409茂名市\440902茂南区\440902-2数据库及成果图件\数据库成果\ARCGIS\梅江区耕地质量等级成果补充完善数据库1028.mdb", 0);
            //if (pWorkSpace == null)
            //    return null;
            //IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)pWorkSpace;
            //IEnumDataset pEnumDataset;
            //IEnumFeatureClass pEnumFeatureClass;
            //IFeatureClassContainer pFeatureclassContainer;
            //IDataset pDataset;
            //IFeatureClass pFeatureClass;
            //pFeatureClass = null;
            //IFeatureDataset pFeatureDataset;
            //IWorkspace2 pWorkspace2;
            //pWorkspace2 = (IWorkspace2)pWorkSpace;
            //pEnumDataset = (IEnumDataset)pWorkSpace.get_Datasets(esriDatasetType.esriDTFeatureDataset);//获得MDB端的所有数据集
            //pDataset = (IDataset)pEnumDataset.Next();
            //IFeatureLayer m_featureLayer = new FeatureLayer();
            //while (pDataset != null)//遍历数据集
            //{
            //    pFeatureDataset = (IFeatureDataset)pDataset;
            //    if (pFeatureDataset != null)
            //    {
            //        pFeatureclassContainer = (IFeatureClassContainer)pFeatureDataset;
            //        pEnumFeatureClass = (IEnumFeatureClass)pFeatureclassContainer.Classes;
            //        pFeatureClass = (IFeatureClass)pEnumFeatureClass.Next();
            //        while (pFeatureClass != null)//在每一个数据集中遍历数据层IFeatureClass
            //        {
            //            break;
            //        }
            //        break;
            //    }
            //}
            //return pFeatureClass;
            ////m_featureLayer.FeatureClass = pFeatureClass;
            ////return m_featureLayer;
            #endregion

            #region 只读取mdb数据库中的耕地分等单元数据
            if (workspacePath == null) return null;
            if (!File.Exists(workspacePath)) return null;
            IWorkspaceFactory workspaceFactory = new AccessWorkspaceFactoryClass();
            IWorkspace pWorkSpace = workspaceFactory.OpenFromFile(workspacePath, 0);
            //IWorkspace pWorkSpace = workspaceFactory.OpenFromFile(@"E:\县级耕地质量等级成果数据库(空库)工作简报6修改1.mdb", 0);
            if (pWorkSpace == null)
                return null;
            string mdbfileName = System.IO.Path.GetFileNameWithoutExtension(workspacePath);
            IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)pWorkSpace;
            IEnumDataset pEnumDataset;
            IEnumFeatureClass pEnumFeatureClass;
            IFeatureClassContainer pFeatureclassContainer;
            IDataset pDataset;
            IFeatureClass pFeatureClass;
            pFeatureClass = null;
            IFeatureDataset pFeatureDataset;
            IWorkspace2 pWorkspace2;
            pWorkspace2 = (IWorkspace2)pWorkSpace;
            pEnumDataset = (IEnumDataset)pWorkSpace.get_Datasets(esriDatasetType.esriDTFeatureDataset);//获得MDB端的所有数据集
            pDataset = (IDataset)pEnumDataset.Next();
            IFeatureLayer featureLayer = new FeatureLayer();
            if (pDataset != null)
            {
                while (pDataset != null)//遍历数据集
                {
                    pFeatureDataset = (IFeatureDataset)pDataset;
                    if (pFeatureDataset != null)
                    {
                        pFeatureclassContainer = (IFeatureClassContainer)pFeatureDataset;
                        pEnumFeatureClass = (IEnumFeatureClass)pFeatureclassContainer.Classes;
                        pFeatureClass = (IFeatureClass)pEnumFeatureClass.Next();
                        if (pFeatureClass.Fields.FieldCount > 175)
                        {
                            while (pFeatureClass != null)//在每一个数据集中遍历数据层IFeatureClass
                            {
                                break;
                            }
                            break;
                        }
                        else
                        {
                            pEnumDataset = pWorkSpace.get_Datasets(esriDatasetType.esriDTFeatureClass);
                            pEnumDataset.Reset();
                            pDataset = pEnumDataset.Next();
                            while (pDataset != null)
                            {
                                pFeatureClass = pDataset as IFeatureClass;
                                if (pFeatureClass.Fields.FieldCount < 175)
                                {
                                    pFeatureClass = null;
                                    pDataset = pEnumDataset.Next();
                                }
                                else
                                {
                                    break;
                                }
                            }
                            break;
                        }
                    }
                    pDataset = pEnumDataset.Next();
                }
            }
            else
            {
                //加载矢量数据
                pEnumDataset = pWorkSpace.get_Datasets(esriDatasetType.esriDTFeatureClass);
                pEnumDataset.Reset();
                pDataset = pEnumDataset.Next();
                while (pDataset != null)
                {
                    pFeatureClass = pDataset as IFeatureClass;
                    if (pFeatureClass.Fields.FieldCount < 175)
                    {
                        pFeatureClass = null;
                        pDataset = pEnumDataset.Next();
                    }
                    else
                    {
                        break;
                    }
                }
            }
            featureLayer.FeatureClass = pFeatureClass;
            featureLayer.Name = mdbfileName;
            return featureLayer;
            #endregion
        }

        /// <summary>通过IDataStatistics获取图层指定字段所有值
        /// </summary>
        /// <param name="pFeatureLayer"></param>
        /// <param name="fieldName"></param>
        /// <returns>指定字段所有值</returns>
        public static List<string> GetLayerFieldValueByDataStatistics(IFeatureLayer pFeatureLayer, string fieldName,string whereClause)//获取图层字段唯一值（2）
        {
            IQueryFilter queryFilter = new QueryFilterClass();
            queryFilter.SubFields = fieldName;
            queryFilter.WhereClause = whereClause;
            IFeatureCursor featureCursor = pFeatureLayer.FeatureClass.Search(queryFilter, true);
            IDataStatistics dataStatistics = new DataStatisticsClass();
            dataStatistics.Field = fieldName;
            dataStatistics.Cursor = featureCursor as ICursor;
            System.Collections.IEnumerator pEnumvar = dataStatistics.UniqueValues;
            pEnumvar.Reset();
            object obj = null;
            List<string> uniqueValueList = new List<string>();
            while (pEnumvar.MoveNext())
            {
                obj = pEnumvar.Current;
                uniqueValueList.Add(obj.ToString());
            }
            uniqueValueList.Sort();
            return uniqueValueList;
        }

        /// <summary>通过图层名在MapControl中查找图层，并返回图层文件存放路径
        /// </summary>
        /// <param name="?"></param>
        /// <param name="?"></param>

        public static string GetFileCategory(AxMapControl pMap, string layerName)
        {
            UID uid = new UIDClass();
            uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";//FeatureLayer
            //{6CA416B1-E160-11D2-9F4E-00C04F6BC78E} IDataLayer （包括所有类型的图层）
            //{40A9E885-5533-11d0-98BE-00805F7CED21} IFeatureLayer
            //{D02371C7-35F7-11D2-B1F2-00C04F8EDEFF} IRasterLayer
            //{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} IGeoFeatureLayer
            //{34B2EF81-F4AC-11D1-A245-080009B6F22B} IGraphicsLayer
            //{5CEAE408-4C0A-437F-9DB3-054D83919850} IFDOGraphicsLayer
            //{0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E} ICoverageAnnotationLayer
            //{EDAD6644-1810-11D1-86AE-0000F8751720} IGroupLayer 
            IEnumLayer layers = pMap.Map.get_Layers(uid, true);
            layers.Reset();
            ILayer layer = null;
            string category = "";
            while ((layer = layers.Next()) != null)
            {
                if (layer.Name == layerName)
                {
                    IDataLayer2 pDL = layer as IDataLayer2;
                    IDatasetName pDN = (IDatasetName)pDL.DataSourceName;
                    category = pDN.WorkspaceName.Category;
                    //path=pDN.WorkspaceName.PathName;
                    return category;
                }
            }
            return "";
        }

        public static void RemoveLayer(AxMapControl mapControl, ILayer pLayer)
        {
            IEnumLayer pEnumLayers = mapControl.Map.Layers;
            ILayer layer = pEnumLayers.Next();
            while (layer != null)
            {
                if (layer == pLayer)
                {
                    mapControl.Map.DeleteLayer(layer);
                }
                layer = pEnumLayers.Next();
            }
        }
        //移除新加图层
        public static void RemoveLayer(AxMapControl mapControl)
        {
            string path = "";
            IEnumLayer pEnumLayers = mapControl.Map.Layers;
            ILayer layer = pEnumLayers.Next();
            while (layer != null)
            {
                IDataLayer2 pDL = layer as IDataLayer2;
                IDatasetName pDN = (IDatasetName)pDL.DataSourceName;
                path = pDN.WorkspaceName.PathName;
                if (path.Contains("44-1县级成果") || path.Contains("44-2省级成果"))
                {
                    mapControl.Map.DeleteLayer(layer);
                }
                layer = pEnumLayers.Next();
            }
        }

        /**/
        /// <summary>
        /// 取得图层的上一级对象，可能是igrouplayer,或ibasicmap
        /// </summary>
        /// <param name="map"></param>
        /// <param name="layer"></param>
        /// <returns></returns>
        public static object GetLayerParent(IBasicMap map, ILayer layer)
        {
            ILayer tmpLayer;
            for (int i = 0; i <= map.LayerCount - 1; i++)
            {
                tmpLayer = map.get_Layer(i);
                if (tmpLayer == layer)
                {
                    return map;
                }
                else if (tmpLayer is IGroupLayer)
                {
                    IGroupLayer ly = FindParentGroupLayer(tmpLayer as IGroupLayer, layer);
                    if (ly != null)
                        return ly;
                }
            }
            return map;
        }
        public static IGroupLayer FindParentGroupLayer(IGroupLayer groupLayer, ILayer layer)
        {
            if (!(groupLayer is ICompositeLayer))
            {
                return groupLayer;
            }
            ICompositeLayer comLayer = groupLayer as ICompositeLayer;
            ILayer tmpLayer;
            for (int i = 0; i <= comLayer.Count - 1; i++)
            {
                tmpLayer = comLayer.get_Layer(i);
                if (tmpLayer == layer)
                    return groupLayer;
                else if (tmpLayer is IGroupLayer)
                    return FindParentGroupLayer(tmpLayer as IGroupLayer, layer);
            }
            return null;
        }

        public static IGroupLayer getGroupLayerByName(AxMapControl mapControl, string layerName)
        {
            if (mapControl == null) return null;
            for (int i = 0; i < mapControl.LayerCount; i++)
            {
                if (mapControl.get_Layer(i).Name.Equals(layerName))
                {
                    if (mapControl.get_Layer(i) is IGroupLayer)
                    {
                        return (IGroupLayer)mapControl.get_Layer(i);
                    }
                }
                else continue;
            }
            return null;
        }


        /// <summary>
        /// 数据编辑公共变量
        /// </summary>
        public static IFeature dataEditSelcetfeature = null;
        public static string dataEditOperatorString = null;
        public static List<IFeature> selectFeatureList = new List<IFeature>(); 
    }
}
